<template>
  <div>
    <div class="box">
      <h3>账号密码登录</h3>
    </div>
    <van-form @submit="onSubmit">
      <van-cell-group inset>
        <van-field
          v-model="username"
          name="username"
          placeholder="用户名"
          :rules="[{ required: true, message: '请填写用户名' }]"
        />
        <van-field
          v-model="password"
          type="password"
          name="password"
          placeholder="密码"
          :rules="[{ required: true, message: '请填写密码' }]"
        />
      </van-cell-group>
      <div style="margin: 16px">
        <van-button round block type="primary" native-type="submit">
          登录
        </van-button>
      </div>
    </van-form>
    <div class="box2">
      <span>验证码登录</span>|<span>忘记密码</span>
    </div>
  </div>
</template>

<script setup>

import axios from "axios";
import { ref } from "vue";
import { showNotify } from "vant";
import { useRouter } from "vue-router";

const router = new useRouter();
const username = ref("");
const password = ref("");
const onSubmit = async(values) => {
    const state=await axios.post('/api/login', values)
    console.log(state);
    const {code}=state.data;
    if(code===10000){
        localStorage.setItem('user',JSON.stringify(values))
        router.push('/home')
        showNotify({ type: "success", message: "登录成功" });
    }else{
        showNotify({ type: "success", message: "登录失败" });
    }
};
</script>

<style lang="scss" >

*{
  margin: 0;
  padding: 0;
}
.box{
  color: rgb(97, 108, 224);
  border-radius: 10px;
  padding: 20px;
  margin-top: 20px;
}
.box2{
  text-align: center;
  margin-top: 20px;
}
</style>